package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.OrderIdGeneratorService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.VisitingrecordTb;
import com.ruoyi.system.service.IVisitingrecordTbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 来访记录Controller
 * 
 * @author ruoyi
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/visiting/visiting")
public class VisitingrecordTbController extends BaseController
{
    @Autowired
    private IVisitingrecordTbService visitingrecordTbService;

    @Autowired
    private OrderIdGeneratorService orderIdGeneratorService;


    /**
     * 查询来访记录列表
     */
    @PreAuthorize("@ss.hasPermi('visiting:visiting:list')")
    @GetMapping("/list")
    public TableDataInfo list(VisitingrecordTb visitingrecordTb)
    {
        startPage();
        System.out.println("接收的数据："+visitingrecordTb);
        List<VisitingrecordTb> list = visitingrecordTbService.selectVisitingrecordTbList(visitingrecordTb);
        return getDataTable(list);
    }



    /**
     * 导出来访记录列表
     */
    @PreAuthorize("@ss.hasPermi('visiting:visiting:export')")
    @Log(title = "来访记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VisitingrecordTb visitingrecordTb)
    {
        List<VisitingrecordTb> list = visitingrecordTbService.selectVisitingrecordTbList(visitingrecordTb);
        ExcelUtil<VisitingrecordTb> util = new ExcelUtil<VisitingrecordTb>(VisitingrecordTb.class);
        util.exportExcel(response, list, "来访记录数据");
    }

    /**
     * 获取来访记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('visiting:visiting:query')")
    @GetMapping(value = "/{receiveId}")
    public AjaxResult getInfo(@PathVariable("receiveId") String receiveId)
    {
        return success(visitingrecordTbService.selectVisitingrecordTbByReceiveId(receiveId));
    }

    /**
     * 新增来访记录
     */
    @PreAuthorize("@ss.hasPermi('visiting:visiting:add')")
    @Log(title = "来访记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VisitingrecordTb visitingrecordTb)
    {
        //KF-XXXXXXXX-XXXX
        String visitingId;
        if (visitingrecordTb.getReceiveSource().equals("1") ){
            visitingId = orderIdGeneratorService.generateOrderId("KF","KF");
        }else {
            visitingId = orderIdGeneratorService.generateOrderId("WZ","WZ");
        }
        visitingrecordTb.setReceiveId(visitingId);
//        System.out.println("接收来源："+visitingrecordTb.getReceiveSource());
        System.out.println("数据："+visitingrecordTb.toString());
//        System.out.println("生成的ID："+visitingId);
        return toAjax(visitingrecordTbService.insertVisitingrecordTb(visitingrecordTb));
    }

    /**
     * 修改来访记录
     */
    @PreAuthorize("@ss.hasPermi('visiting:visiting:edit')")
    @Log(title = "来访记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VisitingrecordTb visitingrecordTb)
    {
        return toAjax(visitingrecordTbService.updateVisitingrecordTb(visitingrecordTb));
    }

    /**
     * 删除来访记录
     */
    @PreAuthorize("@ss.hasPermi('visiting:visiting:remove')")
    @Log(title = "来访记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{receiveIds}")
    public AjaxResult remove(@PathVariable String[] receiveIds)
    {
        return toAjax(visitingrecordTbService.deleteVisitingrecordTbByReceiveIds(receiveIds));
    }
}
